using BasicScript.Component;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class EyeMachineDisplayerItem : BaseItem
	{
		private enum TouchType
		{
			Static = 0,
			LeftTouch = 1,
			RightTouch = 2
		}

		private float SCALE_MIN_Y = -0.8f;

		private float SCALE_MAX_Y = 0.8f;

		private float SCALE_Y = 0.8f;

		private TouchType touchType;

		private Transform displayer;

		private Transform btnLeft;

		private Transform leftScale;

		private Transform btnRight;

		private Transform rightScale;

		private float lastTouchY;

		private float leftScaleY;

		private float rightScaleY;

		private float targetLeftY;

		private float targetRightY;

		private SpriteRenderer blurImage;

		private SpriteAssets spriteAssets;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			btnLeft = model.Find("BtnLeft");
			btnLeft.gameObject.layer = 15;
			leftScale = btnLeft.Find("Scale");
			btnRight = model.Find("BtnRight");
			btnRight.gameObject.layer = 15;
			rightScale = btnRight.Find("Scale");
			displayer = model.Find("Displayer");
			displayer.gameObject.SetActive(false);
			blurImage = displayer.Find("BlurImage").GetComponent<SpriteRenderer>();
			blurImage.transform.SetLocalZ(-1E-05f);
			SetLeftScaleY(data.floatParam1);
			SetRightScaleY(data.floatParam2);
			spriteAssets = model.GetComponent<SpriteAssets>();
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			base.TouchDown(touchEventData);
			touchType = TouchType.Static;
			RaycastHit2D raycastHit2D = Physics2D.Raycast(touchEventData.curPos, Vector2.zero, 15f, 32768);
			if (raycastHit2D.collider != null)
			{
				if (raycastHit2D.collider.transform == btnLeft)
				{
					touchType = TouchType.LeftTouch;
					lastTouchY = touchEventData.curPos.y;
					Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
				}
				else if (raycastHit2D.collider.transform == btnRight)
				{
					touchType = TouchType.RightTouch;
					lastTouchY = touchEventData.curPos.y;
					Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
				}
			}
			lastTouchY = touchEventData.curPos.y;
		}

		public override void TouchMove(TouchEventData touchEventData)
		{
			base.TouchMove(touchEventData);
			if (touchType == TouchType.LeftTouch)
			{
				SetLeftScaleY(leftScaleY + touchEventData.curPos.y - lastTouchY);
			}
			else if (touchType == TouchType.RightTouch)
			{
				SetRightScaleY(rightScaleY + touchEventData.curPos.y - lastTouchY);
			}
			lastTouchY = touchEventData.curPos.y;
		}

		private void SetLeftScaleY(float y)
		{
			if (y < SCALE_MIN_Y)
			{
				y = SCALE_MIN_Y;
			}
			else if (y > SCALE_MAX_Y)
			{
				y = SCALE_MAX_Y;
			}
			leftScaleY = y;
			leftScale.SetLocalY(leftScaleY);
			CheckBlurImage();
		}

		private void SetRightScaleY(float y)
		{
			if (y < SCALE_MIN_Y)
			{
				y = SCALE_MIN_Y;
			}
			else if (y > SCALE_MAX_Y)
			{
				y = SCALE_MAX_Y;
			}
			rightScaleY = y;
			rightScale.SetLocalY(rightScaleY);
			CheckBlurImage();
		}

		public void SetDisplayerActive(bool val)
		{
			if (displayer != null)
			{
				displayer.gameObject.SetActive(val);
			}
		}

		public void SetImageId(int id)
		{
			if (id == 0)
			{
				blurImage.gameObject.SetActive(false);
				return;
			}
			blurImage.sprite = spriteAssets.GetSprite(id.ToString());
			if (Random.Range(0, 2) == 0)
			{
				if (leftScaleY + SCALE_Y <= SCALE_MAX_Y)
				{
					targetLeftY = leftScaleY + SCALE_Y;
				}
				else
				{
					targetLeftY = leftScaleY - SCALE_Y;
				}
			}
			else if (leftScaleY - SCALE_Y >= SCALE_MIN_Y)
			{
				targetLeftY = leftScaleY - SCALE_Y;
			}
			else
			{
				targetLeftY = leftScaleY + SCALE_Y;
			}
			if (Random.Range(0, 2) == 0)
			{
				if (rightScaleY + SCALE_Y <= SCALE_MAX_Y)
				{
					targetRightY = rightScaleY + SCALE_Y;
				}
				else
				{
					targetRightY = rightScaleY - SCALE_Y;
				}
			}
			else if (rightScaleY - SCALE_Y >= SCALE_MIN_Y)
			{
				targetRightY = rightScaleY - SCALE_Y;
			}
			else
			{
				targetRightY = rightScaleY + SCALE_Y;
			}
			blurImage.gameObject.SetActive(true);
			CheckBlurImage();
		}

		private void CheckBlurImage()
		{
			float value = Mathf.Abs(targetLeftY - leftScaleY) * 0.006f + Mathf.Abs(targetRightY - rightScaleY) * 0.006f;
			value = Mathf.Clamp(value, 0f, 0.01f);
			blurImage.material.SetFloat("_Offset", value);
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["62"] = leftScaleY;
			obj["63"] = rightScaleY;
		}
	}
}
